#!/bin/bash

if [ $# -ne 1 ]
then
	echo "Usage: $0 number_of_tests"
	exit
fi

number_of_tests=$1

mean_value=0.0;
real_time=0.0;

for (( i=0 ; i<number_of_tests ; i++ ))
do
	time_before=`date +%s`
	./o | tail -n 2 > result
	time_after=`date +%s`
	real_time=`echo $real_time + $time_after - $time_before | bc`

	var=`cat result | cut -d ' ' -f 2|tail -n 1`
	array[$i]=$var
	mean_value=`echo $mean_value + $var | bc`
	sleep 1
done

mean_value=`echo "scale=2; $mean_value / $number_of_tests" | bc`
real_time=`echo "scale=2; $real_time / $number_of_tests" | bc`
#mean_value=`echo scale=4; ${mean_value} | bc`

#calculate standard_deviation
standard_deviation=0.0;
i=0
#echo "dev: $standard_deviation"
while [ $i -lt ${#array[*]} ]; 
do
#	echo "$i: ${array[$i]}"
	standard_deviation=`echo "scale=5; $standard_deviation + (${array[$i]} - $mean_value)^2" | bc`
#	echo "dev: $standard_deviation"
	let i++
done
standard_deviation=`echo "scale=2; sqrt($standard_deviation/$number_of_tests)" | bc`
#	echo "dev: $standard_deviation"


echo "Results from $number_of_tests execution(s):"
echo "	average solution: $mean_value"
echo "	standard deviation: $standard_deviation"
echo "	average time: $real_time seconds"
